package chapter10;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/19 15:01
 */
public class LruCache<K, V> extends LinkedHashMap<K, V> {
    private static final long serialVersionUID = -4739325330490105730L;
    private int maxEntries;

    public LruCache(int maxEntries) {
        super(16, 0.75f, true);
        this.maxEntries = maxEntries;
    }

    public static void main(String[] args) {
        LruCache<String, Object> cache = new LruCache<>(3);

        cache.put("a", "abstract");
        cache.put("b", "basic");
        cache.put("c", "call");
        cache.get("a");
        cache.put("d", "call");

        System.out.println(cache);

        cache.forEach((k, v) -> {
            System.out.println(k + ":" + v);
        });
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return size() > maxEntries;
    }
}
